package by.artureg.so.conf;

import java.util.Calendar;
import java.util.Date;

/**
 * Author: Arthur Poleschuk
 * When: 01.02.12
 */
public class Const {
    public static final String INTEGER = "integer";
    public static final String DOUBLE = "double";
    public static final String DATETIME = "datetime";
    public static final String DATE = "date";
    public static final String STRING = "string";
    public static final String DECIMAL = "decimal";
    public static final String BOOL = "bool";

    public static final String NOW = "@now"; // today (date) or current time(datetime)
    public static final String PMB = "@prevmonthbeg"; // - beginning of previous month
    public static final String PME = "@prevmonthend"; // - end of previous month. Beware of difference in date and datetime (last day of month is 30/31, but last datetime of month is <00:00:00 01-of-nextmonth)
    public static final String CMB = "@currmonthbeg"; // - beginning of current month
    public static final String CME = "@currmonthend"; // - end of current month
    public static final String PYB = "@prevyearbeg"; // - beginning of previous year
    public static final String PYE = "@prevyearend"; // - end of previous year
    public static final String CYB = "@curryearbeg"; // - beginning of current year
    public static final String CYE = "@curryearend"; // - end of current year

    public static Date defDate(String s) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        if (NOW.equals(s)) {
            return new Date();
        } else if (PMB.equals(s)) {
            calendar.add(Calendar.MONTH, -1);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            return calendar.getTime();
        } else if (PME.equals(s)) {
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MILLISECOND, 999);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            return calendar.getTime();
        } else if (CMB.equals(s)) {
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            return calendar.getTime();
        } else if (CME.equals(s)) {
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MILLISECOND, 999);
            calendar.add(Calendar.MONTH, 1);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            return calendar.getTime();
        } else if (PYB.equals(s)) {
            calendar.add(Calendar.YEAR, -1);
            calendar.set(Calendar.MONTH, 0);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            return calendar.getTime();
        } else if (PYE.equals(s)) {
            calendar.set(Calendar.MONTH, 0);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.add(Calendar.MILLISECOND, -1);
            return calendar.getTime();
        } else if (CYB.equals(s)) {
            calendar.set(Calendar.MONTH, 0);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            return calendar.getTime();
        } else if (CYE.equals(s)) {
            calendar.add(Calendar.YEAR, 1);
            calendar.set(Calendar.MONTH, 0);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.add(Calendar.MILLISECOND, -1);
            return calendar.getTime();
        }

        return new Date();
    }

    public static void main(String[] args) {
        System.out.println(defDate(PMB));
        System.out.println(defDate(PME));
        System.out.println(defDate(CMB));
        System.out.println(defDate(CME));
        System.out.println(defDate(PYB));
        System.out.println(defDate(PYE));
        System.out.println(defDate(CYB));
        System.out.println(defDate(CYE));
    }

}
